import heapq
class Solution(object):
    def scheduleCourse(self, courses):
        courses.sort(key=lambda x: (x[1], x[0]))
        now = 0
        st = []
        for c, t in courses:
            if now + c <= t:
                now += c
                heapq.heappush(st, - c)
            else:
                if st and - st[0] > c:
                    node = heapq.heappop(st)
                    heapq.heappush(st, - c)
                    now += node + c
        return len(st)

